﻿using FluentValidation;
using SpotsHub.Gateway.Models.Customer;
using Nop.Core.Domain.Customers;
using Nop.Services.Localization;
using Nop.Services.Logistics;

namespace SpotsHub.Gateway.Validators
{
    public class RegisterValidator : AbstractValidator<RegisterModel>
    {
        public RegisterValidator(ILocalizationService localizationService, CustomerSettings customerSettings, ILogisticsService logisticsService)
        {
            //const string phoneRegex = @"^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[{ ,\-,.}]?([^0-1]){1}([0-9]){2}[{ ,\-,.}]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$";

            RuleFor(x => x.Email).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Email.Required"));
            RuleFor(x => x.Email).EmailAddress().WithMessage(localizationService.GetResource("Common.WrongEmail"));
            RuleFor(x => x.FirstName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.FirstName.Required"));
            RuleFor(x => x.LastName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.LastName.Required"));


            RuleFor(x => x.Password).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Password.Required"));
            RuleFor(x => x.Password).Length(customerSettings.PasswordMinLength, 999).WithMessage(string.Format(localizationService.GetResource("Account.Fields.Password.LengthValidation"), customerSettings.PasswordMinLength));
            RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ConfirmPassword.Required"));
            RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage(localizationService.GetResource("Account.Fields.Password.EnteredPasswordsDoNotMatch"));


            //form fields
            //if (customerSettings.CompanyRequired && customerSettings.CompanyEnabled)
            //{
            //    RuleFor(x => x.Company).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Company.Required"));
            //}
            //if (customerSettings.StreetAddressRequired && customerSettings.StreetAddressEnabled)
            //{
            //    RuleFor(x => x.StreetAddress).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StreetAddress.Required"));
            //}
            //if (customerSettings.StreetAddress2Required && customerSettings.StreetAddress2Enabled)
            //{
            //    RuleFor(x => x.StreetAddress2).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StreetAddress2.Required"));
            //}
            //if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
            //{
            //    RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));

            //    RuleFor(x => x.ZipPostalCode)
            //        .Matches(@"^ *[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1} *$")
            //        .WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Invalid"));

            //    RuleFor(x => x.ZipPostalCode)
            //        .Must(zp => (logisticsService.GetZoneByZipPostalCode(zp, "CA") != null))
            //        .WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.OutOfService"));
            //}

            //if (customerSettings.StateProvinceEnabled)
            //{
            //    RuleFor(x => x.StateProvinceId).GreaterThan(0).WithMessage(localizationService.GetResource("Account.Fields.StateProvince.Required"));

            //    RuleFor(x => x.StateProvinceId)
            //        .Must((model, x) => { 
            //            if (!string.IsNullOrEmpty(model.ZipPostalCode))
            //            {
            //                var zone = logisticsService.GetZoneByZipPostalCode(model.ZipPostalCode, "CA");
            //                if (zone != null)
            //                {
            //                    return x == zone.District.StateProvinceId;
            //                }
            //            }
            //            return true;
            //        })
            //        .WithMessage(localizationService.GetResource("Account.Fields.StateProvince.PostalCodeMismatch"));
            //}

            //if (customerSettings.CityRequired && customerSettings.CityEnabled)
            //{
            //    RuleFor(x => x.City).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.City.Required"));
            //}
            //if (customerSettings.CellPhoneRequired && customerSettings.CellPhoneEnabled)
            //{
            //    //+string+
            //    RuleFor(x => x.CellPhone).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Cellphone.Required"));
            //}
            //if (customerSettings.CellPhoneRequired && customerSettings.CellPhoneEnabled)
            //{
            //    RuleFor(x => x.CellPhone)
            //        .Matches(phoneRegex)
            //        .WithMessage("The cell phone number is invalid.");
            //}
            //if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
            //{
            //    RuleFor(x => x.Phone).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Phone.Required"));
            //}
            //if (customerSettings.PhoneEnabled)
            //{
            //    RuleFor(x => x.Phone)
            //        .Matches(phoneRegex)
            //        .When(x => !string.IsNullOrEmpty(x.Phone))
            //        .WithMessage("The phone number is invalid.");
            //}
            //if (customerSettings.FaxRequired && customerSettings.FaxEnabled)
            //{
            //    RuleFor(x => x.Fax).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Fax.Required"));
            //}
        }
    }
}